const app = getApp()
const $api = require('../../../utils/api.js').API;
Page({


  data: {
    scrollHeight: '100vh',
    uid:0,
    page:1,
    list:[],
    touid:0,
    uimg:'',
    toimg:'',
    setInter:'',
    inputValue:'',
    scrollHeight: '100vh',
    toView:'',
    role:0,
    wordShow:false,
    words:{},
    showArr:[]
  },
  onLoad: function(options){
    var touid = options.touid
    this.setData({
      touid:touid,
      uid:wx.getStorageSync('uid'),
      uimg:wx.getStorageSync('avatarUrl'),
      role:wx.getStorageSync('role')
    })
    this.getList()
    this.getInfo()
  },
  onShow: function(){
    var that = this
    this.data.setInter = setInterval(function(){
      that.getList();
    },2000)
  },
  getInfo:function(){
    var that = this
    var data = {'uid':this.data.touid}
    $api.get('member/status',data).then(res => {
      //请求成功
      if(res.data.code == 1){
        that.setData({
          toimg:res.data.data.avatar
        })
        }
    })
  },
  getList:function(){
    var that = this
    var data = {'uid':wx.getStorageSync('uid'),'touid':this.data.touid}
    $api.get('online/list',data).then(res => {
      //请求成功
      if(res.data.code == 1){
        var str = res.data.data.length - 1;
        that.setData({
          list:res.data.data,
          toView:'A'+ str
        })
      }
    })
  },
  messageAdd:function(){
    this.send()
  },
  bindKeyInput:function(e){
    this.setData({
      inputValue: e.detail.value
    })
  },
  onShareAppMessage: function(){
    return {
        title: "天意美业直通车",
        path: "/pages/index/index?puid="+wx.getStorageSync('uid')
    };
  },
  onUnload: function() {
    var that = this
    clearInterval(that.data.setInter)
    that.setData({
      setInter:''
    })
  },
  onHide:function(){
    var that = this
    clearInterval(that.data.setInter)
    that.setData({
      setInter:''
    })
  },
  sceollTop:function(e){
    var that = this
    var type = e.detail.deltaY
    if(type > 0){//上滑动
      clearInterval(that.data.setInter)
      that.setData({
        setInter:''
      })
    }else{//下滑动
      if(!that.data.setInter){
        that.data.setInter = setInterval(function(){
          that.getList();
        },2000)
      }
    }
  },
  send:function(){
    if(!this.data.inputValue){
      return;
    }
    var that = this
    var data = {'uid':wx.getStorageSync('uid'),'touid':this.data.touid,'content':this.data.inputValue}
    var list = that.data.list
    $api.post('online/add',data).then(res =>{
      that.setData({
        'inputValue':'',
        list:list.concat(data)
      })
    })
  },
  toSearch:function(){
    this.send()
  },
  wordsOpen:function(){
    var data = {'uid':wx.getStorageSync('uid')}
    var that = this
    $api.get('problem/words',data).then(res =>{
      if(res.data.code == 1){
        that.setData({
          wordShow:true,
          words:res.data.data
        })
      }
    })
  },
  wordsClose:function(){
    this.setData({
      wordShow:false
    })
  },
  wordsSend:function(e){
    var value = e.target.dataset.remark
    this.setData({
      'inputValue':value,
      wordShow:false
    })
    this.send()
  },
  gowords:function(){
    wx.navigateTo({
      url: '/pages/message/online/words'
  })
  },
  longtap:function(e){
    //this.showArr[index] = true
    var index = e.currentTarget.dataset.index
    var showArr = []
    showArr[index] = true
    this.setData({
      showArr:showArr
    })
  },
  closeShow:function(e){
    var index = e.currentTarget.dataset.index
    var showArr = []
    showArr[index] = false
    this.setData({
      showArr:showArr
    })
  },
  addWord:function(e){
    var index = e.currentTarget.dataset.index
    var content = e.currentTarget.dataset.content
    var data = {
      uid:wx.getStorageSync('uid'),
      remark:content
    }
    var that = this
    $api.post('problem/wordsAdd',data).then(res =>{
      wx.showToast({
        title:'添加成功',
        duration:3000,
        mask:true,
        icon:'success', 
      })
      var showArr = []
      showArr[index] = false
      that.setData({
        showArr:showArr
      })
    })
  }
})